package com.example.springboottestgit.service;

import com.alibaba.fastjson.JSONObject;
import com.example.springboottestgit.bean.BusinessException;
import com.example.springboottestgit.bean.EconomicContentQaRecord;
import com.example.springboottestgit.bean.dto.EconomicPolicyDTO;
import com.example.springboottestgit.bean.vo.EconomicPolicyVO;
import com.example.springboottestgit.bean.vo.InputVO;
import com.example.springboottestgit.dao.EconomicContentQaRecordMapper;
import com.example.springboottestgit.enums.ExceptionCodeEnum;
import com.example.springboottestgit.utils.HttpUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.time.Duration;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * @description:
 * @author: ZZZ
 */
@Slf4j
@Service
public class EconomicPolicyModelService {

    @Value("${economic.policy.mode.url}")
    private String economicPolicyModeUrl;

    @Value("${economic.horizon.mode.url}")
    private String economicHorizonModeUrl;

    @Autowired
    private EconomicContentQaRecordMapper economicContentQaRecordMapper;


    public long save(EconomicPolicyDTO dto) {
        EconomicContentQaRecord insert = new EconomicContentQaRecord();
        insert.setType(dto.getType());
        if (dto.getId() != null) {
            insert.setAssoId(dto.getId());
        }

        insert.setQuestion(dto.getQuestion());
        economicContentQaRecordMapper.insert(insert);
        return insert.getId();

    }

    @Async("myAsync")
    public void economicPolicyAsk(EconomicPolicyDTO dto) {
        log.info("===========经济大模型提问=============");
        LocalDateTime s = LocalDateTime.now();
        EconomicContentQaRecord update = new EconomicContentQaRecord();
        update.setId(dto.getId());
        InputVO inputVO = new InputVO();
        inputVO.setQuestion(dto.getQuestion());
        inputVO.setStartYear(dto.getStartYear());
        inputVO.setEndYear(dto.getEndYear());
        String param = JSONObject.toJSONString(inputVO);
        //1-经济政策；2-经济视界
        String url = "1".equals(dto.getType()) ? economicPolicyModeUrl : economicHorizonModeUrl;
        String result = null;
        try {
            log.info("参数：{}", param);
            result = HttpUtils.post(url, param);
            log.info("回答：{}", result);
            update.setStatus(1);
            update.setAnswer(result);
            LocalDateTime e = LocalDateTime.now();
            Duration between = Duration.between(s, e);
            economicContentQaRecordMapper.updateById(update);
            log.info("经济大模型接口调用耗时 {} 秒", between.getSeconds());
        } catch (Exception e) {
            update.setStatus(1);
            update.setAnswer("AI 机器人繁忙中，请先休息一下，稍后再提问吧");
            economicContentQaRecordMapper.updateById(update);
            log.error("经济大模型接口调用失败", e);
            throw new BusinessException(ExceptionCodeEnum.INTERFACE_CALL_FAIL.code, "经济大模型接口调用失败");
        }
    }


    public EconomicPolicyVO economicPolicyQuery(long id) {
        return economicContentQaRecordMapper.economicPolicyQuery(id);
    }

    public List<EconomicPolicyVO> economicPolicyQueryList(long assoId) {
        List<EconomicPolicyVO> list = new ArrayList<>();
        EconomicPolicyVO vo = economicContentQaRecordMapper.economicPolicyQuery(assoId);
        if (vo != null) {
            list.add(vo);
            list.addAll(economicContentQaRecordMapper.economicPolicyQueryList(assoId));
        }
        return list;
    }
}
